//
//  PywLoginStatusView.m
//  PywSdk
//
//  Created by Jade on 4/7/16.
//  Copyright © 2016 zero. All rights reserved.
//

#import "PywLoginStatusView.h"
#import "PywMasonry.h"
#import "UIImage+Pyw.h"
#import "PywLoginTopView.h"
#import "PywMBProgressHUD+Pyw.h"
#import "PywSdk+login.h"
#import "PywCPNotifications.h"
#import "PywPlatformMain.h"
#import "PywFileManager.h"

#import "PywRealNameAuthManager.h"
#import "PywUploadAccountLoginManager.h"

/*登录中*/

@interface PywLoginStatusView()
@property (nonatomic,strong) UIView *middleView;
@property (nonatomic,strong) UILabel *statusLabel;
@property (nonatomic,strong) UIButton *switchButton;
@property (nonatomic,assign) BOOL isStopBinding;//停止显示绑定手机提示
@end

@implementation PywLoginStatusView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUp];
    }
    return self;
}

#pragma mark - setUp

- (void)setUp
{
    [self addSubview:self.middleView];
    [self.middleView addSubview:self.statusLabel];
    [self.middleView addSubview:self.switchButton];
    self.backgroundColor = RGB(1, 1, 1, .8);
    self.alpha = 0;
    [self addConstraints];
}
/**
 *  添加约束
 */
- (void)addConstraints
{
    WEAK_SELF;
    [self.middleView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.mas_centerX);
        make.top.bottom.equalTo(weakSelf);
        make.width.mas_equalTo(270);
    }];
    [self.statusLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.middleView.mas_centerY);
        make.left.equalTo(weakSelf.middleView.mas_left);
    }];
    
    [self.switchButton mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.middleView.mas_centerY);
        make.right.equalTo(weakSelf.middleView.mas_right);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(90);
    }];
}



#pragma mark - getter

- (UIView *)middleView
{
    if (!_middleView) {
        _middleView = [UIView new];
    }
    return _middleView;
}

- (UILabel *)statusLabel
{
    if (!_statusLabel) {
        _statusLabel = [UILabel new];
        _statusLabel.font = [UIFont systemFontOfSize:14.f];
        _statusLabel.textColor = [UIColor whiteColor];
        _statusLabel.text = @"欢迎登录...";
        _statusLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _statusLabel;
}


- (UIButton *)switchButton
{
    if (!_switchButton) {
        _switchButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _switchButton.layer.cornerRadius = 4.f;
        _switchButton.layer.masksToBounds = YES;
        _switchButton.backgroundColor = UIColorFromRGB(0xAAAAAA);
        [_switchButton setTitle:@"切换账号"
                       forState:UIControlStateNormal];
        [_switchButton addTarget:self
                          action:@selector(selectSwitchButton:)
                forControlEvents:UIControlEventTouchUpInside];
        [_switchButton setTitleColor:[UIColor blackColor]
                            forState:UIControlStateNormal];
        [_switchButton setTitleColor:[UIColor blueColor]
                            forState:UIControlStateHighlighted];
        [_switchButton setAccessibilityIdentifier:@"switch_account"];
        
    }
    return _switchButton;
}


#pragma mark - Action Method

/**
 *  更新Frame
 */
- (void)updateStatusFrame
{
    NSString *statusName = [NSString stringWithFormat:@"欢迎登录，%@",[PywPlatformMain sharedPlatformMain].userName];
    self.statusLabel.text = statusName;
    [self addConstraints];
}

/**
 *  切换账号
 *
 *  @param sender <#sender description#>
 */
- (void)selectSwitchButton:(UIButton *)sender{
    
    self.isStopBinding = YES;
    if (self.switchOtherLoginCompletion) {
        self.switchOtherLoginCompletion(self);
    }
}

/**
 *  登录成功
 */
- (void)loginSuccess
{
    if (self.isStopBinding) {
        return;
    }
    if (self.loginCompletion) {
        self.loginCompletion(self);
    }
    
    //清除切换账号信息
    [PywPlatformMain sharedPlatformMain].changeAccount = @"";
    [[PywFileManager manager] removeObjectForKey:@"changeAccount"];
    
    [PywMBProgressHUD showSuccess:@"已登录" toView:[UIApplication sharedApplication].keyWindow];
    // 关闭时才发送登录完成的通知
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    // 发送登录完成的通知
    [dict setObject:[NSNumber numberWithInt:PYW_LOGIN_STATE_SUCCESS] forKey:@"result"];
    [dict setObject:@"登录成功" forKey:@"info"];
    [dict setObject:[PywPlatformMain sharedPlatformMain].account forKey:@"account"];
    // 发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:kPywCPLoginNotification object:nil userInfo:dict];
    
    // 登录成功，保存当前账号登录时间,并查询是否上传登录时长
    [[PywUploadAccountLoginManager manager] uploadAccountLoginTimeLong];
    
    // Modal出“实名认证”控制器，加载Web端实名制认证
    [[PywRealNameAuthManager manager] showRealNameAuthViewController];
}



/**
 *  是否停止绑定手机的提示
 *
 *  @param isStopBinding <#isStopBinding description#>
 */
- (void)updateStopBindingAlert:(BOOL)isStopBinding
{
    self.isStopBinding = isStopBinding;
}

#pragma mark - Public
/**
 *  添加到父视图
 *
 *  @param superView 添加到哪个view
 */
- (void)add2SuperView:(UIView *)superView
{
    [superView addSubview:self];
    [self mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.left.right.and.top.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    
    [self updateStatusFrame];
    
    
    [self performSelector:@selector(loginSuccess)
               withObject:nil
               afterDelay:4.f];
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self endEditing:YES];
}


@end
